home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / BodyView.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  194 lines

  1. /*
  2.  * @(#)BodyView.java    1.6   98/03/13
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing.text.html;
  21.  
  22. import java.util.Enumeration;
  23. import java.awt.*;
  24. import java.net.*;
  25. import java.awt.image.*;
  26. import com.sun.java.swing.border.*;
  27. import com.sun.java.swing.text.*;
  28. import com.sun.java.swing.*;
  29. import java.awt.event.*;
  30.  
  31. /**
  32.  *
  33.  * @author  Makarand Gokhale
  34.  * @version 1.6 03/13/98
  35.  */
  36. class BodyView extends BoxView  {
  37.  
  38.     transient Image bgimage;
  39.     transient Color bgcolor;
  40.     transient String colorStr = null;
  41.     transient String imageStr = null;
  42.     HTMLRootImageObserver imageObserver=null;
  43.  
  44.     boolean bImageLoaded=false;
  45.     boolean bImageLoadFailed=false;
  46.  
  47.  
  48.     int imgWidth = -1;
  49.     int imgHeight = -1;
  50.     int drawWidth = -1;
  51.     int drawHeight = -1;
  52.  
  53.  
  54.     public BodyView(Element elem, int axis) {
  55.     super(elem, axis);
  56. // Disable until Time gets this working
  57. //    loadBackground(elem.getAttributes());
  58.     }
  59.  
  60.     public void loadBackground ( AttributeSet as) {
  61.     imageStr = (String) as.getAttribute("background");
  62.         colorStr = (String ) as.getAttribute("bgcolor");
  63.     bImageLoaded = false;
  64.     bImageLoadFailed = false;
  65.     imgWidth = imgHeight = drawWidth = drawHeight = -1;
  66.         bgimage = null;
  67.        if ( colorStr == null )
  68.        bgcolor = Color.lightGray;
  69.        else {
  70.        bgcolor = Utilities.stringToColor(colorStr);
  71.        }
  72.        if(imageStr != null) {
  73.        imageObserver = new HTMLRootImageObserver();
  74.        bgimage = loadImage(imageStr);
  75.        bImageLoaded = false;
  76.     }
  77.           
  78.     }
  79.  
  80.     private Image loadImage(String imageStr) {
  81.         if( imageStr==null ) return null;
  82.         URL url = null;
  83.         URL reference = (URL) getElement().getDocument().getProperty(Document.StreamDescriptionProperty);
  84.     if(reference != null ) {
  85.         try {
  86.         url = new URL(reference,imageStr);
  87.         } catch (MalformedURLException e) {
  88.         System.out.println("Malformed URL:"+reference);
  89.         }
  90.     }
  91.     if(url != null )
  92.         return Toolkit.getDefaultToolkit().getImage(url);
  93.     else
  94.         return Toolkit.getDefaultToolkit().getImage(imageStr);
  95.  
  96.     }
  97.    
  98.  
  99.     public void paint(Graphics g, Shape allocation) {
  100. //  Disable painting until Time gets it  to paint via HTMLEditorKit
  101. //    paintBackground( g, allocation);
  102.     super.paint(g,allocation);
  103.     }
  104.     public void paintBackground(Graphics g, Shape allocation ) {
  105.     Element elem = getElement();
  106.     AttributeSet as = elem.getAttributes();
  107.     String cStr = (String) as.getAttribute("bgcolor");
  108.     String iStr = (String) as.getAttribute("background");
  109.     if( cStr != null) {
  110.         if(!cStr.equals(colorStr))
  111.             loadBackground(as);
  112.     }
  113.     else if (colorStr != null)
  114.         loadBackground(as);
  115.     if( iStr != null) {
  116.          if(!iStr.equals(imageStr))
  117.             loadBackground(as);
  118.     }
  119.     else if (imageStr != null )
  120.         loadBackground(as);
  121.  
  122.     Rectangle alloc = allocation.getBounds();
  123.     setSize(alloc.width, alloc.height);
  124.         if(bgimage != null){
  125.          if (bImageLoaded) {
  126.          for(int xpos=0;xpos < alloc.width; xpos+=imgWidth)  {
  127.             for(int ypos=0;ypos < alloc.height; ypos+=imgHeight)  {
  128.             g.drawImage(bgimage,xpos,ypos, null);
  129.             }
  130.          }
  131.         }
  132.         else {
  133.              paintBGColor(g, allocation);
  134.          if(!bImageLoadFailed)
  135.              g.drawImage(bgimage,0,0, imageObserver);
  136.         }
  137.         if(!bImageLoadFailed) {
  138.         if(imgWidth <= 0 )
  139.             imgWidth = bgimage.getWidth(imageObserver);
  140.         if(imgHeight <= 0 )
  141.             imgHeight = bgimage.getHeight(imageObserver);
  142.         }
  143.     }
  144.         else {
  145.         paintBGColor(g, allocation);
  146.         }
  147.     }
  148.  
  149.     private void paintBGColor(Graphics g, Shape allocation ) {
  150.     Rectangle alloc = allocation.getBounds();
  151.         g.setColor(bgcolor);
  152.         g.fillRect(0,0, alloc.width, alloc.height);
  153.     }
  154.  
  155.  
  156.  
  157.  
  158.     class  HTMLRootImageObserver  implements ImageObserver {
  159.  
  160.     public HTMLRootImageObserver() {
  161.         super();
  162.     }
  163.  
  164.      public boolean imageUpdate(Image img,
  165.                                      int infoflags,
  166.                                      int x,
  167.                                      int y,
  168.                                      int width,
  169.                                      int height) {
  170.         if((infoflags & ERROR) > 0 || (infoflags & ABORT) > 0 )
  171.             bImageLoadFailed = true;
  172.         if((infoflags & WIDTH) > 0 ) {
  173.              imgWidth = width;
  174.         }
  175.         if((infoflags & HEIGHT) > 0 ) {
  176.              imgHeight = height;
  177.         }
  178.         if((infoflags & SOMEBITS) > 0 || (infoflags & ALLBITS) > 0  ) {
  179.              drawWidth = width>drawWidth?width:drawWidth;
  180.              drawHeight = height>drawHeight?height:drawHeight;
  181.         }
  182.         if(imgWidth > 0 && imgHeight > 0 ) {
  183.             if(imgWidth == drawWidth && imgHeight == drawHeight) {
  184.             bImageLoaded = true;
  185.                 getContainer().repaint();
  186.             }
  187.         }
  188.         return !bImageLoaded;
  189.     }
  190.     }
  191.  
  192.  
  193. }
  194.